package com.example.petshopping.ui.orders.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.petshopping.R;
import com.example.petshopping.ui.orders.OrderDetailActivity;
import com.example.petshopping.ui.orders.model.Order;

import java.util.ArrayList;
import java.util.List;

public class SimpleOrderAdapter extends RecyclerView.Adapter<SimpleOrderAdapter.OrderViewHolder> {

    private Context context;
    private List<Order> orderList;

    public SimpleOrderAdapter(Context context, List<Order> orderList) {
        this.context = context;
        this.orderList = new ArrayList<>(orderList);
    }

    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.item_order_new, parent, false);
        return new OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        Order order = orderList.get(position);

        // 设置订单基本信息
        holder.tvStoreName.setText(order.getStoreName());
        holder.tvOrderStatus.setText(order.getStatusText());
        holder.tvProductTitle.setText(order.getProductTitle());
        holder.tvProductPrice.setText(order.getPrice());
        holder.tvProductQuantity.setText("x" + order.getQuantity());

        // 设置店铺头像 - 优先使用URL，否则使用资源ID
        if (!TextUtils.isEmpty(order.getStoreAvatarUrl())) {
            // 使用Glide加载店铺头像
            Glide.with(context)
                    .load(order.getStoreAvatarUrl())
                    .placeholder(R.drawable.yugang) // 加载中显示的默认图片
                    .error(R.drawable.yugang) // 加载失败显示的默认图片
                    .circleCrop() // 圆形头像
                    .into(holder.ivStoreAvatar);
        } else if (order.getStoreAvatarResId() > 0) {
            holder.ivStoreAvatar.setImageResource(order.getStoreAvatarResId());
        } else {
            // 使用默认头像
            holder.ivStoreAvatar.setImageResource(R.drawable.yugang);
        }

        // 设置商品图片 - 优先使用URL，否则使用资源ID
        if (!TextUtils.isEmpty(order.getProductImageUrl())) {
            // 使用Glide加载网络图片
            Glide.with(context)
                    .load(order.getProductImageUrl())
                    .placeholder(R.drawable.dog) // 加载中显示的默认图片
                    .error(R.drawable.dog) // 加载失败显示的默认图片
                    .centerCrop()
                    .into(holder.ivProductImage);
        } else if (order.getProductImageResId() > 0) {
            holder.ivProductImage.setImageResource(order.getProductImageResId());
        } else {
            // 使用默认商品图片
            holder.ivProductImage.setImageResource(R.drawable.dog);
        }
        // 设置订单总金额(实付款)
        holder.tvTotalAmount.setText(order.getTotalAmount());

        // 设置按钮文本和点击事件
        setupButtons(holder, order);

        // 设置整个条目点击事件
        holder.itemView.setOnClickListener(v -> {
            try {
                // 跳转到订单详情页
                Intent intent = new Intent(context, OrderDetailActivity.class);
                intent.putExtra("order_id", order.getId());
                context.startActivity(intent);
            } catch (Exception e) {
                Toast.makeText(context, "无法打开订单详情", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setupButtons(OrderViewHolder holder, Order order) {
        // 根据订单状态设置不同的按钮文本
        switch (order.getStatus()) {
            case Order.STATUS_PENDING_CONFIRM:
                holder.btnLeft.setText("查看详情");
                holder.btnRight.setText("去结算");

                holder.btnLeft.setOnClickListener(v ->
                        Toast.makeText(context, "查看拍卖商品详情", Toast.LENGTH_SHORT).show());
                holder.btnRight.setOnClickListener(v ->
                        Toast.makeText(context, "去结算", Toast.LENGTH_SHORT).show());
                break;
            case Order.STATUS_PENDING_PAYMENT:
                holder.btnLeft.setText("取消订单");
                holder.btnRight.setText("继续付款");

                holder.btnLeft.setOnClickListener(v ->
                        Toast.makeText(context, "已取消订单", Toast.LENGTH_SHORT).show());
                holder.btnRight.setOnClickListener(v ->
                        Toast.makeText(context, "前往支付页面", Toast.LENGTH_SHORT).show());
                break;

            case Order.STATUS_PENDING_SHIPMENT:
                holder.btnLeft.setText("联系卖家");
                holder.btnRight.setText("提醒发货");

                holder.btnLeft.setOnClickListener(v ->
                        Toast.makeText(context, "联系卖家功能", Toast.LENGTH_SHORT).show());
                holder.btnRight.setOnClickListener(v ->
                        Toast.makeText(context, "已提醒卖家发货", Toast.LENGTH_SHORT).show());
                break;

            case Order.STATUS_PENDING_RECEIPT:
                holder.btnLeft.setText("查看物流");
                holder.btnRight.setText("确认收货");

                holder.btnLeft.setOnClickListener(v ->
                        Toast.makeText(context, "查看物流信息", Toast.LENGTH_SHORT).show());
                holder.btnRight.setOnClickListener(v ->
                        Toast.makeText(context, "确认收货成功", Toast.LENGTH_SHORT).show());
                break;

            case Order.STATUS_COMPLETED:
                holder.btnLeft.setText("申请退款");
                holder.btnRight.setText("再次购买");

                holder.btnLeft.setOnClickListener(v ->
                        Toast.makeText(context, "申请退款功能", Toast.LENGTH_SHORT).show());
                holder.btnRight.setOnClickListener(v ->
                        Toast.makeText(context, "加入购物车成功", Toast.LENGTH_SHORT).show());
                break;

            case Order.STATUS_REFUND:
                holder.btnLeft.setText("查看详情");
                holder.btnRight.setText("联系客服");

                holder.btnLeft.setOnClickListener(v ->
                        Toast.makeText(context, "查看退款详情", Toast.LENGTH_SHORT).show());
                holder.btnRight.setOnClickListener(v ->
                        Toast.makeText(context, "联系客服功能", Toast.LENGTH_SHORT).show());
                break;
        }
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    public void updateData(List<Order> newOrders) {
        this.orderList.clear();
        if (newOrders != null) {
            this.orderList.addAll(newOrders);
        }
        notifyDataSetChanged();
    }

    static class OrderViewHolder extends RecyclerView.ViewHolder {
        TextView tvStoreName, tvOrderStatus, tvProductTitle, tvProductPrice, tvProductQuantity,tvTotalAmount;
        ImageView ivStoreAvatar, ivProductImage;
        Button btnLeft, btnRight;

        public OrderViewHolder(@NonNull View itemView) {
            super(itemView);

            ivStoreAvatar = itemView.findViewById(R.id.iv_store_avatar);
            tvStoreName = itemView.findViewById(R.id.tv_store_name);
            tvOrderStatus = itemView.findViewById(R.id.tv_order_status);
            tvProductTitle = itemView.findViewById(R.id.tv_product_title);
            tvProductPrice = itemView.findViewById(R.id.tv_product_price);
            tvProductQuantity = itemView.findViewById(R.id.tv_product_quantity);
            ivProductImage = itemView.findViewById(R.id.iv_product_image);
            btnLeft = itemView.findViewById(R.id.btn_left);
            btnRight = itemView.findViewById(R.id.btn_right);
            tvTotalAmount = itemView.findViewById(R.id.tv_total_amount);
        }
    }
} 